package kr.saeildamil.androidstudy.ch17;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;

public class MyView extends View {
	Paint mPaint;
	ArrayList<Vertex> arVertex;

	public MyView(Context context) {
		super(context);

		// paint 객체 미리 초기화;
		mPaint = new Paint();
		mPaint.setColor(Color.BLACK);
		mPaint.setStrokeWidth(3);
		mPaint.setAntiAlias(true);

	}

	public MyView(Context context, ArrayList<Vertex> arVertex) {
		super(context);
		this.arVertex = arVertex;

		// paint 객체 미리 초기화;
		mPaint = new Paint();
		mPaint.setColor(Color.BLACK);
		mPaint.setStrokeWidth(3);
		mPaint.setAntiAlias(true);
	}

		@Override
		protected void onDraw(Canvas canvas) {
			canvas.drawColor(0xffe0e0e0);
			
			// 정점을 순회하면서 선분으로 잇는다;
			for(int i = 0; i<arVertex.size();i++){
				if(arVertex.get(i).Draw){
					canvas.drawLine(arVertex.get(i-1).x, arVertex.get(i-1).y, arVertex.get(i).x, arVertex.get(i).y, mPaint);
				}
			}
	}
		
		@Override
		public boolean onTouchEvent(MotionEvent event) {
			if(event.getAction()==MotionEvent.ACTION_DOWN){
				arVertex.add(new Vertex(event.getX(), event.getY(), false));
				return true;
			}if(event.getAction()==MotionEvent.ACTION_MOVE){
				arVertex.add(new Vertex(event.getX(), event.getY(), true));
				invalidate();
				return true;
			}
			return false;
		}
}

